#ifndef TICKETS_H_
#define TICKETS_H_
#define MAX_FILEL		128
#define ETICKET_LEN		40
#define TICKET_LEN		33

typedef struct{
	int dtime;
	int etime;
	int ftime;
	char id[ETICKET_LEN];
	int retries;
}ticketP;

typedef char * ticketT;

/**
 ** Gets the parameters of the provided ticket md5 ID.
 **
 **	@param	database	The name of database to consult.
 **	@param	md5			The TICKET's ID.
 **	@param	tickp		Structure that contains the ticket parameters;
 ** @return 			OK if no error NOT_OK if error or ticket not exists.
 **/
int GetTicketParameters(char * database, char * md5, ticketP * tickp);

/**
 ** Marks the ticket provided as removed in the database.
 **
 **	@param	database	The name of database to consult.
 **	@param	md5			The TICKET's ID.
 ** @return 			Returns OK if ticket was removed.
 **
 **/
int RemoveTicket(char * database, ticketT md5);

/**
 ** Increase the retries of requests for exchange for a ticket that hasn't been
 **	enabled.
 **
 **	@param	database	The name of database to consult.
 **	@param	md5			The TICKET's ID.
 **	@param	tickp		Structure that contains the ticket parameters.
 ** @return 			Returns OK if retries were increased or NOT_OK.
 **/
int IncreaseRetries(char * database, ticketT md5,ticketP * tickp);

/**
 ** Generates a TICKET ID by creating an md5 from a string formed by the filename,
 **	process id and the ticket's delivery time.
 **
 **	@param	filename	Filename of the requested file.
 **	@param	pid			Process ID of the server process.
 **	@param	dtime		Delivery time.
 ** @return				Returns the ticket ID or NULL if an error ocurred;
 **
 **/
char * createId(char * filename, int pid, int dtime);

/**
 ** Checks the database to see if the ticket exists.
 **
 **	@param	database	The name of database to consult.
 **	@param	id			The TICKET's ID.
 **	@param	ip			IP of the client that requested the ticket¡s exchange.
 ** @return 			Returns TRUE if the ticket exists ir FALSE if it doesn't
 **						or an error ocurred.
 **/
short TicketExists(char * database, char * id, char * ip);

/**
 ** Increase the retries of requests for exchange for a ticket that hasn't been
 **	enabled.
 **
 **	@param	database	The name of database to consult.
 **	@param	id			The TICKET's ID.
 **	@param	ip			IP of the client that requested the ticket¡s exchange.
 ** @return 			Returns OK if ticket created or NOT_OK if an error 
 **						ocurred.
 **/
int CreateTicket(char * database, char * filename, int pid, ticketP * tickp, 
				char * ip);
				
/**
 ** Check's the database to see if the given user has tickets left for the day
 **
 **	@param	database	The name of database to consult.
 **	@param	login		User login ID.
 ** @return 			Returns the number of tickets left for the day negative
 **						indicates error.
 **/
int TicketsLeft(char * database, int login);

/**
 ** Increases the amount of tickets requested by a given user for the day
 **
 **	@param	database	The name of database to consult.
 **	@param	login		User login ID.
 ** @return 			Returns NOT_OK if there weren't any tickets requested
 **						for the day by the user or a DB error ocurred.
 **/
int IncTickets(char * database, int login);


#endif /*TICKETS_H_*/
